package org.hcyspartnoc.luoxin.controller.searchtopic;


import cn.hutool.db.PageResult;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.common.CommonUtilsException;
import org.hcyspartnoc.luoxin.common.exception.searchtopic.QuestionException;
import org.hcyspartnoc.luoxin.pojo.dto.searchtopic.*;
import org.hcyspartnoc.luoxin.pojo.vo.searchtopic.QuestionTypesVO;
import org.hcyspartnoc.luoxin.pojo.vo.searchtopic.QuestionVo;
import org.hcyspartnoc.luoxin.service.searchtopic.QuestionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Resource
    private QuestionService  questionService;

    //单个添加
    @Log(title = "题目·添加·单个", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "question:insert:single", descr = "添加单个题目")
    @PostMapping("/insert/single")
    public AjaxResult insertQuestion(@RequestBody QuestionDto questionDto) throws QuestionException {
        questionService.insertQuestion(questionDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "题目·类型·获取", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "question:get:types", descr = "获取全部题目类型")
    @GetMapping("/get/types")
    public AjaxResult getQuestionTypes() throws QuestionException{
        List<QuestionTypesVO> questionTypesVO = questionService.getQuestionTypes();
        return AjaxResult.success(questionTypesVO);
    }

    @Log(title = "题目·添加·excel", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "questions:insert:excel", descr = "批量添加excel题目")
    @PostMapping("/insert/excel")
    public AjaxResult insertQuestionsByExcel(@Valid @ModelAttribute QuestionFileDto questionFileDto) throws QuestionException {
        questionService.insertQuestionsByExcel(questionFileDto);
        return AjaxResult.successMsg();
    }
    @Log(title = "题目·添加·word", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "questions:insert:word", descr = "批量添加word题目")
    @PostMapping("/insert/word")
    public AjaxResult insertQuestionsByWord(@Valid @ModelAttribute QuestionFileDto questionFileDto) throws QuestionException, CommonUtilsException {
        questionService.insertQuestionsByWord(questionFileDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "题目·修改", businessType = BusinessType.UPDATE)
    @PreAuthorize(auth = "question:update", descr = "修改题目")
    @PutMapping ("/update")
    public AjaxResult updateQuestion(@RequestBody QuestionUpdateDto questionUpdateDto) throws QuestionException {
        questionService.updateQuestion(questionUpdateDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "题目·查询", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "question:query", descr = "查询题目")
    @GetMapping("/query")
    public AjaxResult queryQuestion(@RequestBody QuestionSearchDto questionSearchDto){
         List<QuestionVo> questionVo=questionService.queryQuestion(questionSearchDto);
         return AjaxResult.success(questionVo);
    }
    @Log(title = "题目·获取·详情", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "question:query:details", descr = "获取题目详情")
    @GetMapping("/query/{questionId:\\d+}")
    public AjaxResult getQuestionDetails(@NotNull(message = "id不能为空") @PathVariable Long questionId) throws QuestionException {
        QuestionVo questionVo=questionService.getQuestionDetails(questionId);
        return AjaxResult.success(questionVo);
    }
    @Log(title = "题目·查询·分页", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "question:query:page", descr = "分页查询题目")
    @PostMapping("/query/page")
    //应该根据什么去查
    public PageBean queryQuestionPages(QuestionPageDto questionPageDto){
        PageResult pageResult=questionService.queryPageQuestion(questionPageDto);
        return null;
    }
    //删除
    //批量删除
    @Log(title = "题目·删除·ID", businessType = BusinessType.DELETE)
    @PreAuthorize(auth = "question:delete:questionId", descr = "删除题目")
    @DeleteMapping("/delete/{questionId:\\d+}")
    public AjaxResult deleteQuestion(@NotNull(message = "id不能为空") @PathVariable Long questionId) throws QuestionException {
        questionService.deleteQuestion(questionId);
        return AjaxResult.successMsg();
    }
}
